package com.jd.ssdb4j;

import com.jd.ssdb4j.client.SSDB;
import com.jd.ssdb4j.client.SSDBClient;
import com.jd.ssdb4j.stream.pool.PoolSSDBStream;
import com.jd.ssdb4j.stream.pool.SocketSSDBStreamPool;
import com.jd.ssdb4j.stream.replication.ReplicationSSDMStream;
import org.apache.commons.pool.impl.GenericObjectPool.Config;

import java.nio.charset.Charset;

public class SSDBs {
	
	public static String DEFAULT_HOST = "127.0.0.1";
	public static int DEFAULT_PORT = 8888;
	public static int DEFAULT_TIMEOUT = 2000;
	public static Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

	public static final SSDB simple() {
		return simple(DEFAULT_HOST, DEFAULT_PORT, DEFAULT_TIMEOUT);
	}
	
	public static final SSDB simple(String host, int port, int timeout) {
		return new SSDBClient(host, port, timeout);
	}
	
	public static final SSDB pool(String host, int port, int timeout, Config config) {
		if (config == null) {
			config = new Config();
			config.testWhileIdle = true;
		}
		return new SSDBClient(new PoolSSDBStream(new SocketSSDBStreamPool(host, port, timeout, config)));
	}

	public static final SSDB replication(String masterHost, int masterPort, String slaveHost, int slavePort, int timeout, Config config) {
		if (config == null) {
			config = new Config();
			config.testWhileIdle = true;
		}
		PoolSSDBStream master = new PoolSSDBStream(new SocketSSDBStreamPool(masterHost, masterPort, timeout, config));
		PoolSSDBStream slave = new PoolSSDBStream(new SocketSSDBStreamPool(slaveHost, slavePort, timeout, config));
		return new SSDBClient(new ReplicationSSDMStream(master, slave));
	}
}
